package com.bing.bean.weixin;

import com.bing.utils.Constant;
import com.bing.utils.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.List;

/**
 * Created by kaibb on 2014/12/9.
 */

public class Account implements Serializable{
    private Logger logger = LoggerFactory.getLogger(Account.class);
    private int id;
    private String appId;
    private String appSecret;
    private String url;
    private String token;
    private String encodingAESKey;
    private AccessToken accessToken;

    private List<Command> commands;

    private AccountType accountType;
    private String description;
    private AccountStatus accountStatus;

    public List<Command> getCommands() {
        return commands;
    }

    public void setCommands(List<Command> commands) {
        this.commands = commands;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getAppSecret() {
        return appSecret;
    }

    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getEncodingAESKey() {
        return encodingAESKey;
    }

    public void setEncodingAESKey(String encodingAESKey) {
        this.encodingAESKey = encodingAESKey;
    }

    public AccountType getAccountType() {
        return accountType;
    }

    public void setAccountType(AccountType accountType) {
        this.accountType = accountType;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public AccountStatus getAccountStatus() {
        return accountStatus;
    }


    private AccessToken refreshToken() {
        String getTokenUrl = "";
        AccessToken token = new AccessToken();
        switch (accountType) {
            case weixin:
                getTokenUrl = String.format(Constant.WEIXIN_ACCESS_TOKEN, appId, appSecret);
                break;
            case yixin:
                getTokenUrl = String.format(Constant.WEIXIN_ACCESS_TOKEN, appId, appSecret);
                break;

        }
        token = HttpUtils.get(getTokenUrl, AccessToken.class);
        token.setCreateTime(System.currentTimeMillis());
        return token;
    }

    public AccessToken getAccessToken() {
        if (accessToken == null || (System.currentTimeMillis() - accessToken.getExpires_in() * 1000) > accessToken.getCreateTime()) {
            logger.info("请求accessToken:{}", url);
            accessToken = refreshToken();
        }
        return accessToken;
    }

    public void setAccessToken(AccessToken accessToken) {
        this.accessToken = accessToken;
    }

    public void setAccountStatus(AccountStatus accountStatus) {
        this.accountStatus = accountStatus;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Account{");
        sb.append("id=").append(id);
        sb.append(", appId='").append(appId).append('\'');
        sb.append(", appSecret='").append(appSecret).append('\'');
        sb.append(", url='").append(url).append('\'');
        sb.append(", token='").append(token).append('\'');
        sb.append(", encodingAESKey='").append(encodingAESKey).append('\'');
        sb.append(", accessToken=").append(accessToken);
        sb.append(", accountType=").append(accountType);
        sb.append(", description='").append(description).append('\'');
        sb.append(", accountStatus=").append(accountStatus);
        sb.append('}');
        return sb.toString();
    }
}
